<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="close">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="submitChecked">确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListAPI } from '@/api/setting.js'
import { getUserDetailById } from '@/api/user.js'
import { assignRoles } from '@/api/employee.js'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      roleIds: [],
      currentId: ''
    }
  },
  created () {
    this.getLiist()
  },
  methods: {
    // 提交选中结果
    async submitChecked() {
      await assignRoles({ id: this.currentId, roleIds: this.roleIds })
      this.$message.success('提交成功！')
      this.$emit('update:showRoleDialog', false)
    },
    // 回显之前选中的数据
    async recover(id) {
      this.currentId = id
      const { roleIds } = await getUserDetailById(id)
      console.log(roleIds)
      this.roleIds = roleIds
    },
    close() {
      this.$emit('update:showRoleDialog', false)
    },
    async getLiist() {
      const res = await getRoleListAPI(1, 100)
      this.list = res.rows
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      margin-bottom: 20px;
    }
  }
}
</style>
